﻿using NCP.Recruiting.ApplicationServices.Interfaces;
using NCP.Recruiting.ApplicationServices.Messaging.Candidates;
using NCP.Recruiting.ApplicationServices.Shared;
using NCP.Recruiting.Domain.Aggregates.Candidates;
using NCP.Recruiting.Domain.Aggregates.Interviews;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NCP.Recruiting.ApplicationServices.Implementations
{
    public class InterviewAppService : IInterviewAppService
    {
        private readonly IInterviewRepository _interviewRepository;
        private readonly ICandidateRepository _candidateRepository;

        public InterviewAppService(IInterviewRepository interviewRepository,
            ICandidateRepository candidateRepository)
        {
            _interviewRepository = interviewRepository;
            this._candidateRepository = candidateRepository;
        }

        public InvitedToInterviewResponse InvitedToInterview(InvitedToInterviewRequest request)
        {
            try
            {
                var interview = new Interview
                {
                    CompanyId = request.CompanyId,
                    DateCreated = DateTime.Now,
                    Deleted = false,
                    Jobseekers = request.Jobseekers,
                    LastUpdated = DateTime.Now,
                    Body = request.Body,
                    Date = request.Date,
                    InvitedOn = DateTime.Now,
                    Location = request.Location,
                    Time = request.Time,
                    Subject = request.Subject
                };
                interview.GenerateNewIdentity();

                _interviewRepository.Add(interview);

                _candidateRepository.UpdateItem(request.Id, (item) =>
                {
                    item.Status = 3;
                });

                _interviewRepository.UnitOfWork.Commit();

                return new InvitedToInterviewResponse
                {
                    IsSuccess = true
                };
            }
            catch (Exception ex)
            {
                return new InvitedToInterviewResponse
                {
                    Exception = ex,
                    IsSuccess = false
                };
            }
        }
    }
}
